package com.xinxin.facelinker.domain;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easemob.chatuidemo.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.activity.ImageBrowserActivity;
import com.xinxin.facelinker.activity.ShowUserMotionActivity;
import com.xinxin.facelinker.adapter.GridViewMotionPhotoAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gengliang on 2015/7/26.
 */
public class PalsMotionAdapter extends BaseAdapter {

    private Context context;
    private List<ShowPalsMotion> list;
    private ImageLoader imageLoader = ImageLoader.getInstance();

    public PalsMotionAdapter(Context context, List<ShowPalsMotion> list) {
        this.context = context;
        this.list = list;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public List<ShowPalsMotion> getList() {
        return list;
    }

    public void setList(ArrayList<ShowPalsMotion> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout linearLayout;

        if (convertView != null) {
            linearLayout = (LinearLayout) convertView;
        } else {
            linearLayout = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.layout_pals_motion, null);
        }

        final ShowPalsMotion showPalsMotion = getList().get(position);

        View palsMotionTitle = linearLayout.findViewById(R.id.palsMotionTitle);
        View gvMotionImage = linearLayout.findViewById(R.id.gvMotionImage);
        ImageView ivTitleImage = (ImageView) palsMotionTitle.findViewById(R.id.ivTitleImage);
        TextView tvTitleNickname = (TextView) palsMotionTitle.findViewById(R.id.tvTitleNickname);
        TextView tvTitleDate = (TextView) palsMotionTitle.findViewById(R.id.tvTitleDate);
        TextView tvMotion = (TextView) linearLayout.findViewById(R.id.tvMotion);
        GridView gvImage = (GridView) gvMotionImage.findViewById(R.id.gvImage);
        ImageView ivImage = (ImageView) gvMotionImage.findViewById(R.id.ivImage);

        imageLoader.displayImage(showPalsMotion.getPhoto_url(), ivTitleImage);
        tvTitleNickname.setText(showPalsMotion.getNickname());
        tvTitleDate.setText(showPalsMotion.getDate());
        tvMotion.setText(showPalsMotion.getMotion());

        ivTitleImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, ShowUserMotionActivity.class);
                intent.putExtra(Config.KEY_OTHER_ACCOUNT_NUM,showPalsMotion.getAccount_num());
                context.startActivity(intent);
            }
        });


        if (showPalsMotion.getMotion_photo_url() != null && showPalsMotion.getMotion_photo_url().length > 1) {
            gvImage.setVisibility(View.VISIBLE);
            ivImage.setVisibility(View.GONE);

            GridViewMotionPhotoAdapter gridViewMotionPhoto = new GridViewMotionPhotoAdapter(showPalsMotion.getMotion_photo_url(), getContext());
            gvImage.setAdapter(gridViewMotionPhoto);

            gvImage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(context, ImageBrowserActivity.class);
                    intent.putExtra("motionPhotoUrl", showPalsMotion.getMotion_photo_url());
                    intent.putExtra("position", position);
                    context.startActivity(intent);
                }
            });

        } else if (showPalsMotion.getMotion_photo_url() != null && showPalsMotion.getMotion_photo_url().length == 1) {
            gvImage.setVisibility(View.GONE);
            ivImage.setVisibility(View.VISIBLE);

            imageLoader.displayImage(showPalsMotion.getMotion_photo_url()[0], ivImage);

            ivImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context,ImageBrowserActivity.class);
                    intent.putExtra("motionPhotoUrl",showPalsMotion.getMotion_photo_url());
                    context.startActivity(intent);
                }
            });

        } else if (showPalsMotion.getMotion_photo_url() == null) {
            gvImage.setVisibility(View.GONE);
            ivImage.setVisibility(View.GONE);
        }

        return linearLayout;
    }
}
